<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar title="登录" class="page-nav-bar">
      <template #left>
        <van-icon name="arrow-left" color="#fff" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <!-- 表单 -->
    <!-- maxlength 最大11位 -->
    <van-form @submit="onSubmit" ref="lofinForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        :rules="userForm.phone"
        placeholder="请输入手机号"
        type="number"
        maxlength="11"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field
        v-model="user.code"
        type="text"
        name="code"
        placeholder="请输入验证码"
        :rules="userForm.code"
      >
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <template #button>
          <van-button
            size="small"
            @click="fCode"
            class="send-sms-btn"
            round
            type="primary"
            native-type="button"
          >
            <template #default>
              <p v-if="!isShow">发送短信验证码</p>
              <van-count-down
                v-else
                :time="1000 * 60"
                @finish="isShow = false"
                format="ss 秒"
              />
            </template>
          </van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button round block type="info" native-type="submit">
          登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>

import { login, userCode } from '@/api/login'
export default {
  name: 'Login',
  data () {
    return {
      isShow: false,
      user: {
        mobile: '',
        code: ''
      },
      userForm: {
        phone: [{
          required: true,
          message: '请填写手机号'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        code: [{
          required: true,
          message: '请填写验证码'
        }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      }
    }
  },
  methods: {
    // 登录
    async onSubmit () {
      // 获取表单数据
      // 发出请求
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间,默认2000ms
      })
      try {
        const result = await login(this.user)
        console.log(result)
        // 存储到vuex和本地存储中
        this.$store.commit('USERTOKEN', result.data)
        // vant中的组件方法都会挂载到vue的原型上，在组件中使用vant的方法时，
        // 要用this.$...
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (err) {
        // 这个项目失败是通过http状态码来判断的
        // 获取不到服务器的数据
        // 这个err是报错信息,并不是后台返回的数据
        // console.log(err)
        if (err.response.status === 400) {
          this.$toast.fail('登录失败')
        }
      }
    },
    // 发送短信验证码
    async fCode () {
      // 校验手机号
      try {
        await this.$refs.lofinForm.validate('mobile')
        console.log('success')
      } catch (error) {
        return console.log(error)
      }
      // 显示倒计时
      this.isShow = true
      // 发送请求
      try {
        await userCode(this.user.mobile)
        this.$toast.success('发送成功')
      } catch (error) {
        this.isShow = false
        this.$toast.fail('发送失败')
        if (error.response.status === 429) {
          return console.log('发送太频繁')
        }
        return console.log('发送失败')
      }
    }
  }
}
</script>

<style  scoped lang='less'>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    height: 54px;
    line-height: 54px;
    background-color: #ededed;
    font-size: 20px;
    color: #666;
    border: 0;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
